package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class CheckGroupController {
    @Autowired
    CheckGroupService checkGroupService;

    @PostMapping("/checkgroup/add")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.addCheckGroup(checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/checkgroup/delete")
    public Result deleteByIdCheckGroup(String id) {
        try {
            log.info("根据id删除检查组：{}", id);
            checkGroupService.deleteByIdCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @PostMapping("/checkgroup/edit")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.editCheckGroup(checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/checkgroup/findById")
    public Result findByIdCheckGroup(String id) {
        CheckGroup checkGroup = null;
        try {
            checkGroup = checkGroupService.findByIdCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, checkGroup);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    @PostMapping("/checkgroup/findPage")
    public PageResult findPageCheckGroup(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }
}
